/*
 * Copyright 2009 Original Author(s)
 * 
 * This file is part of Kommando
 * 
 * Kommando is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Kommando is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Kommando.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kommando.filesystem.prefs;

import java.awt.Window;
import java.util.Collections;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import org.kommando.core.controlcenter.ApplicationDialog;
import org.kommando.filesystem.core.FileSource;

import net.miginfocom.swing.MigLayout;

/**
 * @author Peter De Bruycker
 */
public abstract class FileSourceDialog extends ApplicationDialog {

    private JTextField sourceFolderField;
    private JCheckBox includeSourceCheckBox;
    private JCheckBox includeHiddenFilesCheckBox;
    private JTextField depthField;
    private JPanel panel;

    public FileSourceDialog(Window parent) {
        super(parent, "Add File Source");
    }

    @Override
    protected JComponent createDialogContent() {
        if (panel == null) {
            panel = new JPanel(new MigLayout("wrap 3", "[right][grow][]", ""));

            JLabel sourceFolderLabel = new JLabel("Source Folder:");
            sourceFolderLabel.setDisplayedMnemonic('s');
            sourceFolderField = new JTextField(30);
            sourceFolderLabel.setLabelFor(sourceFolderField);

            panel.add(sourceFolderLabel);
            panel.add(sourceFolderField, "grow");
            panel.add(new JButton("..."));

            includeSourceCheckBox = new JCheckBox("Include source folder");
            includeSourceCheckBox.setMnemonic('i');
            panel.add(includeSourceCheckBox, "skip, wrap");

            includeHiddenFilesCheckBox = new JCheckBox("Include hidden files");
            includeHiddenFilesCheckBox.setMnemonic('h');
            panel.add(includeHiddenFilesCheckBox, "skip, wrap");

            JLabel depthLabel = new JLabel("Depth:");
            depthLabel.setDisplayedMnemonic('d');
            depthField = new JTextField(2);
            depthLabel.setLabelFor(depthField);

            panel.add(depthLabel);
            panel.add(depthField, "wrap");

            JLabel filterListLabel = new JLabel("Filters:");
            filterListLabel.setDisplayedMnemonic('f');

            JList filterList = new JList();
            filterList.setVisibleRowCount(5);

            filterListLabel.setLabelFor(filterList);

            panel.add(filterListLabel, "left, wrap");
            panel.add(new JScrollPane(filterList), "span 2, grow");

            JPanel buttons = new JPanel(new MigLayout("wrap 1"));

            JButton addButton = new JButton("Add...");
            addButton.setMnemonic('a');
            JButton editButton = new JButton("Edit...");
            editButton.setMnemonic('e');
            JButton removeButton = new JButton("Remove");
            removeButton.setMnemonic('r');

            buttons.add(addButton, "grow");
            buttons.add(editButton, "grow");
            buttons.add(removeButton, "grow");

            panel.add(buttons, "top");
        }

        return panel;
    }

    @Override
    protected void onCancel() {

    }

    @Override
    protected boolean onFinish() {
        String source = sourceFolderField.getText();
        boolean includeSource = includeSourceCheckBox.isSelected();
        boolean includeHidden = includeHiddenFilesCheckBox.isSelected();
        int depth = Integer.parseInt(depthField.getText());

        FileSource fileSource = new FileSource(source, includeSource, includeHidden, depth, Collections
                .<String> emptyList());
        onFinish(fileSource);

        return true;
    }

    public void show(FileSource fileSource) {
        // make sure the components are created
        createDialogContent();

        sourceFolderField.setText(fileSource.getSource());
        includeSourceCheckBox.setSelected(fileSource.isIncludeSource());
        includeHiddenFilesCheckBox.setSelected(fileSource.isIncludeHiddenFiles());
        depthField.setText(String.valueOf(fileSource.getDepth()));

        show();
    }

    protected abstract void onFinish(FileSource fileSource);
}
